import string
from tkinter import N
from tkinter.tix import Tree
from unicodedata import name


class Node:
    def __init__(self,name :string ,type:string = "dir") :
        self.name = name
        self.type = type
        self.children = [] # 文件子目录
        self.parent = None

    def __repr__(self) -> str:
        return self.name


n = Node("hello")
n2 = Node("world")
n.children.append(n)
n2.parent = n

class FileSystemTree:
    def __init__(self) -> None:
        self.root = Node("/")
        self.now = self.root



    def mkdir(self,name):
        # name以斜杠结尾
        if name[-1] != "/":
            name += "/"
        node = Node(name)
        self.now.children.append(node)
        node.parent = self.now

    def ls(self):
        return self.now.children

    def cd(self,name):
        if name[-1] != "/":
            name += "/"
        if name == "../":
            self.now = self.now.parent
            return
        if name == "./":
            self.now = self.now
            return
        for child in self.now.children:
            if child.name == name:
                self.now = child
                return
        raise ValueError("invalid dir")
        


aFileTree = FileSystemTree()
aFileTree.mkdir("init/")
print(aFileTree.now.children)
aFileTree.mkdir("bin/")
aFileTree.mkdir("usr/")

print(aFileTree.ls())

aFileTree.cd("bin")
aFileTree.mkdir("hello")
print(aFileTree.now.children)
aFileTree.cd("hello")
print(aFileTree.now.children)
aFileTree.cd("..")
print(aFileTree.now.children)